<?php
/*
 * 来路分析相关
 * copyright:living10
 */
class Anti_model extends CI_Model{
 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('user_agent');
 	}

	/**
	 * 设置关键词单价
	 * key:searcher_wid_searcher_keyword;
	 * 返回：关键词单价
	 */
 	public function setdata()
 	{
 		$this->anti_model->setWebKeyPriceData();
		$this->anti_model->setWebSettingData();
		$this->anti_model->setWebData();
//		self::setVistorData();
		exit("The Data set success!");
 	}

	/**
	 * 解析搜索引擎过来的关键词
	 * 返回关键词
	 */
	public function getKeywordBySearch($refer, $wid = null) {
		$refer = str_replace('https:','http:',$refer);
		$url_arr = self :: url2Array($refer);
		preg_match("/^(http:\/\/)?([^\/]+)/i", $refer, $matches);
		if (count($matches) > 0) {
			$host = $matches[2];
		} else {
			return;
		}
		$search_arr = array (
			'baidu' => 'wd',
			'google' => 'q',
			'sogou' => 'query'
		);
		foreach ($search_arr as $key => $val) {
			if (strrchr($host, $key)) {
				$keyword = isset ($url_arr[$val]) ? $url_arr[$val] : null;
				$price = $this->redisinit->get('searcher_' . $wid . '_' . $key . '_' . $keyword);
				//$keyword = iconv_set_encoding($keyword, "UTF-8");
				return array (
					'searchEngine' => $key,
					'keyword' => $keyword,
					'price' => $price
				);
				break;
			}
		}
		return null;
	}

		/**
	 * 关键词扣费情况载入
	 */
	public function setWebKeyPriceData($wid = null) {
		$where = "";
		if ($wid)
			$where = " where w.wid=" . $wid;
		$sql = "select w.wid,w.Searcher,w.KeyWord,w.Price from tj_keyword as w " . $where;
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		foreach ($arr as $k => $v) {
			$this->redisinit->set('searcher_' . $v['wid'] . '_' . $v['Searcher'] . '_' . $v['KeyWord'], $v['Price']);
			echo "加载数据：" . 'searcher_' . $v['wid'] . '_' . $v['Searcher'] . '_' . $v['KeyWord'] . "<br>";
			$arr = $this->redisinit->get('searcher_' . $v['wid'] . '_' . $v['Searcher'] . '_' . $v['KeyWord']);
			print_r($arr);
			echo "<br>";
		}
		unset ($arr);
	}

	/**
	 * 加载设置信息
	 */
	public function setWebSettingData($wid = null) {
		$where = "";
		if ($wid)
			$where = " where wid=" . $wid;
		$sql = "select wid,lmtTime,wuxiao_radio,eyi_radio,close_24,close_Hours,close_Times_Jinggao,close_Times_Zuzhi,msgAlarm from tj_setting " . $where;
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		foreach ($arr as $k => $v) {
			$this->redisinit->set('setting_' . $v['wid'], $v);
			echo "加载数据：" . 'setting_' . $v['wid'] . "<br>";
			$arr = $this->redisinit->get('setting_' . $v['wid']);
			print_r($arr);
			echo "<br>";
			unset ($arr);
		}
	}

	/**
	 * 系统内网站信息载入
	 * 调用返回网址注册时间
	 */
	public function setWebData($uid = null,$wid = null) {
		$where = "";
		if($uid && $wid)
		$where = " where uid=".$uid." and wid=".$wid;
		$sql = "select uid,wid,wAddTime from tj_website ".$where;
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		foreach ($arr as $k => $v) {
			$this->redisinit->set('website_' . $v['uid'] . '_' . $v['wid'], array ('build' => date('Y-m-d',$v['wAddTime'])) );
		}
	}

	/**
	 * 加载访客信息
	 */
	public function setVistorData($vid = null) {
		$where = "";
		if ($vid) {
			$where = " where vid=" . $vid;
		}
		$sql = "select vid,state,firstTime,updTime from tj_vistor " . $where;
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		foreach ($arr as $k => $v) {
			$this->redisinit->set('vid_' . $v['vid'], array (
				'build' => date('Y-m-d',$v['wAddTime'])
				));
		}
	}


		/*
	 * 扣费情况写入表
	 */
	public function pay2tj_losebill($data) {
			if(empty($data)) return ;
			$search_data = array ();
			$search_data['Searcher'] = $data['searchEngine'];
			$search_data['KeyWord'] = $data['keyword'];
			$search_data['Price'] = $data['price'];
			$search_data['InsTime'] = date('Y-m-d H:i:s');
			$search_data['wid'] = $data['wid'];
			$this->db->insert('tj_losebill', $search_data);
	}

	/**
	 * 无效改有效
	 */
	 public function Anti()
	 {
		$tj_eid =  trim($this->input->get("eid"));
		$wid =  intval($this->input->get("wid"));
		$uid =  intval($this->input->get("uid"));
 		$eid = explode('-',$tj_eid);
		$vid = intval($eid[0]);
		$data = $this->redisinit->get('wid_vid_'.$wid.'_'.$vid);
		$data['isbad'] = 0;
		$data['bad_count'] = $data['bad_count']-1;
		$this->redisinit->set('wid_vid_'.$wid.'_'.$vid,$data,86400);
	 }

	 /**
	  * 判断是否拦截
	  */
	  public function Attack()
	  {
		$tj_eid =  trim($this->input->get("eid"));
		$wid =  intval($this->input->get("wid"));
		$uid =  intval($this->input->get("uid"));
 		$eid = explode('-',$tj_eid);
		$vid = intval($eid[0]);
		$data = $this->redisinit->get('wid_vid_' . $wid . '_' . $vid);
		$setting = $this->redisinit->get('setting_' . $wid);
		$tanchu = 0;
		$vid_arr = $this->redisinit->get('vid_' . $vid);

		if ($data['eyi_state'] > 10 || @$vid_arr['state'] == 1 || $setting['eyi_radio'] == 3) {
			$tanchu = 1;
		}
		if ($tanchu == 1) {
			$str = 'alert("'.$setting['msgAlarm'].'");
					setTimeout(function()
					{
						window.location.href="http://www.baidu.com";
					}
					,3000);';

			echo $str;
		}
		unset($vid_arr,$setting,$data);
	  }


	/**
	 * 将redis中的用户数据写入表中
	 */
	public function DoRedis2DB()
	{
		$data = $this->redisinit->query('wid_vid_*_*');
		$time =  intval($this->input->get("time")?$this->input->get("time"):1000);
		$now = time()-$time;
		foreach($data as $k=>$v)
		{
			if($v['arriveTime']<$now)
			{
				unset($v['bad_count'],$v['eyi_state']);
				$new_data = $v;
				$new_data['stopTime'] = $new_data['stopTime']>900?900:30;
				$new_data['arriveCount'] = $new_data['arriveCount']+1;
				$this->db->insert('tj_stat',$new_data);
				$this->redisinit->delete('wid_vid_'.$v['wid'].'_'.$v['vid']);
			}
		}
	}
	 /**
	  * 清除所有数据
	  */
	  public function delAllData()
	  {
	  	$this->redisinit->flushAll();
	  }
}
?>
